package com.codeerror.demo.adapter;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.codeerror.demo.R;
import com.codeerror.demo.bean.TodoBean;
import com.codeerror.demo.util.DateUtil;

import java.util.List;

public class TodoTimeLineRecyclerAdapter extends RecyclerView.Adapter<TodoTimeLineRecyclerAdapter.todoHolder> {
    private final List<TodoBean> todoList;

    public TodoTimeLineRecyclerAdapter(List<TodoBean> todoList) {
        this.todoList = todoList;
    }

    @NonNull
    @Override
    public todoHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_ll_time_line, parent, false);
        return new todoHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull todoHolder holder, int position) {
        // 最后一条line不显示
        if (position == todoList.size() - 1) {
            holder.line.setVisibility(View.GONE);
        }else {
            holder.line.setVisibility(View.VISIBLE);
        }
        // 加载数据
        TodoBean todoBean = todoList.get(position);
        holder.title.setText(todoBean.getName());
        if (todoBean.getStartTime() != 0){
            if (todoBean.getEndTime()!=0){
                holder.time.setText(DateUtil.getHourAndMinuteOfTime(todoBean.getStartTime())
                        + " - " + DateUtil.getHourAndMinuteOfTime(todoBean.getEndTime()));
            }else {
                holder.time.setText(DateUtil.getHourAndMinuteOfTime(todoBean.getStartTime()));
            }
        }else {
            holder.time.setVisibility(View.GONE);
        }
        holder.rb.setOnCheckedChangeListener((compoundButton, isChecked) -> {
            if (isChecked){
                holder.dot.setBackgroundResource(R.drawable.dot_time_line_achieved);
                holder.title.setTextColor(Color.GRAY);
                holder.time.setTextColor(Color.GRAY);
                todoBean.setIsAchieved(1);
            }else {
                holder.dot.setBackgroundResource(R.drawable.dot_time_line_not_achieved);
                holder.title.setTextColor(Color.BLACK);
                holder.time.setTextColor(Color.BLACK);
                todoBean.setIsAchieved(0);
            }
            todoBean.save();
        });
    }

    @Override
    public int getItemCount() {
        return todoList.size();
    }

    static class todoHolder extends RecyclerView.ViewHolder {
        TextView title, time;
        RadioButton rb;
        View line, dot;

        public todoHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.tv_todo_title);
            time = itemView.findViewById(R.id.tv_todo_time);
//            rb = itemView.findViewById(R.id.rb_todo_achieved);
//            line = itemView.findViewById(R.id.bottom_line);
            dot = itemView.findViewById(R.id.dot_time_line);
        }
    }
}
